package com.example.mobile2121606110syc0404;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private CheckBox chboxMainYinqing;
    private CheckBox chboxMainBoli;
    private CheckBox chboxMainFangxiangpan;
    private Button btnMainBuy;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        chboxMainYinqing.setOnCheckedChangeListener(this);
        chboxMainBoli.setOnCheckedChangeListener(this);
        chboxMainFangxiangpan.setOnCheckedChangeListener(this);
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg = "选中：";
                if (chboxMainYinqing.isChecked()) {
                    msg += "\n" + chboxMainYinqing.getText().toString();
                }
                if (chboxMainBoli.isChecked()) {
                    msg += "\n" + chboxMainBoli.getText().toString();
                }
                if (chboxMainFangxiangpan.isChecked()) {
                    msg += "\n" + chboxMainFangxiangpan.getText().toString();
                }
                tvMainResult.setText(msg);
            }
        });
    }

    private void initView() {
        chboxMainYinqing = (CheckBox) findViewById(R.id.chboxMainYinqing);
        chboxMainBoli = (CheckBox) findViewById(R.id.chboxMainBoli);
        chboxMainFangxiangpan = (CheckBox) findViewById(R.id.chboxMainFangxiangpan);
        btnMainBuy = (Button) findViewById(R.id.btnMainBuy);
        tvMainResult = (TextView) findViewById(R.id.tvMainResult);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (b) {
            tvMainResult.setText("选中："+compoundButton.getText().toString());
        }else {
            tvMainResult.setText("取消选中："+compoundButton.getText().toString());
        }

    }
}